package eteam.aps.service.impl;

import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.constant.ProductType;
import eteam.aps.common.constant.ScanProType;
import eteam.aps.common.constant.StepType;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.mapper.ScanProAlreadyMapper;
import eteam.aps.mapper.ScanProWithoutMapper;
import eteam.aps.mapper.ScanProcessStepMapper;
import eteam.aps.mapper.SystemMapper;
import eteam.aps.model.dto.ScanProAlreadyDto;
import eteam.aps.model.dto.ScanProDto;
import eteam.aps.model.dto.ScanProWithoutDto;
import eteam.aps.model.entity.BaseEntity;
import eteam.aps.model.entity.ScanProcessStep;
import eteam.aps.model.vo.scan.ScanProAlreadyVo;
import eteam.aps.model.vo.scan.ScanProWithoutVo;
import eteam.aps.service.ProcessPreserveService;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @author LiuLei
 */
@Service
public class ProcessPreserveServiceImpl implements ProcessPreserveService {

    @Autowired
    private ScanProAlreadyMapper scanProAlreadyMapper;

    @Autowired
    private ScanProWithoutMapper scanProWithoutMapper;

    @Autowired
    private LoginUserRedisService loginUserRedisService;

    @Autowired
    private ScanProcessStepMapper scanProcessStepMapper;

    @Autowired
    private SystemMapper systemMapper;

    @Override
    public JsonPage<ScanProAlreadyVo> selAlreadyLis(ScanProAlreadyDto dto) {
        Map<String, Object> map = new HashMap<>(11);
        map.put("state", ParentEntity.FSTATE_DELETED);
        if (Helper.isNotEmpty(dto.getfBarCode())) map.put("barCode", dto.getfBarCode());
        if (Helper.isNotEmpty(dto.getfInvCode())) map.put("invCode", dto.getfInvCode());
        if (Helper.isNotEmpty(dto.getfInvBarCode())) map.put("invBarCode", dto.getfInvBarCode());
        if (Helper.isNotEmpty(dto.getfScaPro())) map.put("wp", dto.getfScaPro());
        if (Helper.isNotEmpty(dto.getfOperator())) map.put("op", dto.getfOperator());
        if (dto.getfProState() != null) map.put("proState", dto.getfProState());
        if (Helper.isNotEmpty(dto.getfMakeTime())) map.put("time", dto.getfMakeTime());
        int count = scanProAlreadyMapper.selAlreadyCount(map);
        if (count == 0) return JsonPage.builder(dto.getPage(), dto.getSize(), 0, Collections.emptyList());
        map.put("offSize", dto.offSize());
        map.put("size", dto.getSize());
        map.put("sort", "FBarCode");
        List<ScanProAlreadyVo> list = scanProAlreadyMapper.selAlreadyLis(map);
        return JsonPage.builder(dto.getPage(), dto.getSize(), count, list);
    }

    @Override
    public JsonPage<ScanProWithoutVo> selWithoutLis(ScanProWithoutDto dto) {
        Map<String, Object> map = new HashMap<>(7);
        map.put("state", ParentEntity.FSTATE_DELETED);
        if (Helper.isNotEmpty(dto.getfBarCode())) map.put("barCode", dto.getfBarCode());
        if (Helper.isNotEmpty(dto.getfInvCode())) map.put("invCode", dto.getfInvCode());
        if (Helper.isNotEmpty(dto.getfScaPro())) map.put("wp", dto.getfScaPro());
        int count = scanProWithoutMapper.selWithoutCount(map);
        if (count == 0) return JsonPage.builder(dto.getPage(), dto.getSize(), 0, Collections.emptyList());
        map.put("offSize", dto.offSize());
        map.put("size", dto.getSize());
        map.put("sort", "FBarCode");
        List<ScanProWithoutVo> list = scanProWithoutMapper.selWithoutLis(map);
        return JsonPage.builder(dto.getPage(), dto.getSize(), count, list);
    }

    @Override
    @Transactional
    public int delAlready(ScanProDto dto) throws Exception {
        VerifyTokenDto user = loginUserRedisService.getUser();
        LocalDateTime now = LocalDateTime.now();
        // 还原数量
        dto.setfLastModifyTime(now);
        dto.setfLastModifyUserId(user.fuserid);
        scanProWithoutMapper.updateQtyWithout(dto);
        // 删除
        Map<String, Object> map = new HashMap<>(5);
        map.put("proState", ScanProType.TEARDOWN);
        map.put("state", ParentEntity.FSTATE_DELETED);
        map.put("modify", user.fuserid);
        map.put("time", now);
        map.put("fId", dto.getfId());
        Map<String, Object> personM = new HashMap<>(2);
        personM.put("State", -1);
        personM.put("id", user.getFpersonid());
        BaseEntity entity = systemMapper.selectEmpNameByUserId(personM);
        ScanProcessStep scanProcessStep = new ScanProcessStep(
                dto.getfInvBarCode(), // 物料条码
                dto.getfScaPro(),  // 工序编号
                dto.getfProName(), // 工序名称
                entity.getId(), // 操作员编号
                entity.getName(), // 操作员名称
                LocalDateTime.now(), // 当前时间
                StepType.DEL_INV, // 操作步骤
                ProductType.TEARDOWN // 产品进程
        );
        Helper.setDefaultAddValue(scanProcessStep, user.getFuserid());
        scanProcessStepMapper.addScanStep(scanProcessStep);
        return scanProAlreadyMapper.undoScanProAlready(map);
    }

    @Override
    @Transactional
    public int delWithout(ScanProDto dto) throws Exception {
        VerifyTokenDto user = loginUserRedisService.getUser();
        LocalDateTime now = LocalDateTime.now();
        // 查询出改条码、工序、物料下的的已扫描的
        Map<String, Object> map = new HashMap<>();
        map.put("fState", ParentEntity.FSTATE_DELETED);
        map.put("proState", ScanProType.OK);
        if (Helper.isNotEmpty(dto.getfBarCode())) map.put("fBarCode", dto.getfBarCode());
        if (Helper.isNotEmpty(dto.getfInvCode())) map.put("fInvCode", dto.getfInvCode());
        if (Helper.isNotEmpty(dto.getfScaPro())) map.put("fWpCode", dto.getfScaPro());
        List<String> list = scanProAlreadyMapper.selAlready(map);
        if (!list.isEmpty()) {
            List<Map<String, Object>> udp = new ArrayList<>(list.size());
            list.forEach(data -> {
                Map<String, Object> mp = new HashMap<>(5);
                mp.put("proState", ScanProType.TEARDOWN);
                mp.put("state", ParentEntity.FSTATE_DELETED);
                mp.put("modify", user.getFuserid());
                mp.put("time", now);
                mp.put("fId", data);
                udp.add(mp);
            });
            if (!udp.isEmpty()) scanProAlreadyMapper.batchUndoScanProAlready(udp);
        }
        // 删除未扫描的数据
        Map<String, Object> dw = new HashMap<>(4);
        if (Helper.isNotEmpty(dto.getfId())) dw.put("id", dto.getfId());
        dw.put("state", ParentEntity.FSTATE_DELETED);
        if (Helper.isNotEmpty(dto.getfId())) dw.put("modify", user.getFuserid());
        dw.put("time", now);
        int del = scanProWithoutMapper.delWithout(dw);
        Map<String, Object> personM = new HashMap<>(2);
        personM.put("State", -1);
        personM.put("id", user.getFpersonid());
        BaseEntity entity = systemMapper.selectEmpNameByUserId(personM);
        ScanProcessStep scanProcessStep = new ScanProcessStep(
                dto.getfBarCode(), // 物料条码
                dto.getfScaPro(),  // 工序编号
                dto.getfProName(), // 工序名称
                entity.getId(), // 操作员编号
                entity.getName(), // 操作员名称
                LocalDateTime.now(), // 当前时间
                StepType.DEL_BAR, // 操作步骤
                ProductType.TEARDOWN // 产品进程
        );
        Helper.setDefaultAddValue(scanProcessStep, user.getFuserid());
        scanProcessStepMapper.addScanStep(scanProcessStep);
        return del;
    }
}
